// import count from '@dfeidao/atom-nodejs/db/count';
import operate from '@dfeidao/atom-nodejs/db/operate';
// import query from '@dfeidao/atom-nodejs/db/query';
import uuid from 'uuid';

interface Message {
	newarr: Array<{
		left: { formid: string, client_type: string, create_time: number, group_number: string; formname: string, package_no: string, package_name: string, package_path: string, productid: string, group_name: string }; right: Array<{
			action_name: string,
			formid: string,
			action_keyword: string,
			code: string,
			productid: string
		}>
	}>;
	tenantid: string;
	productid: string;
	client_type: string;
}

// 保存服务
export default async function atom(msg: Message, action_id: string, session_id: string) {
	const systemid = '12';
	// return msg;

	// const { exec: a, prepare } = query(action_id, session_id, systemid);
	// prepare('pub_form', ['_id', 'formid'], { productid: msg.tenantid, client_type: msg.client_type }, 200, 1, [], []);
	// const [formdata] = await a<D>();
	// const fidarr = new Array();
	// formdata.forEach((v) => {
	// 	fidarr.push(v.formid);
	// });
	const { del, insert: in1, exec: e1 } = operate(action_id, session_id, systemid);
	del('pub_form', { productid: msg.tenantid, client_type: msg.client_type });

	msg.newarr.forEach((v) => {
		if (v.left.formid) {
			del('pub_form_code', { productid: msg.tenantid, formid: v.left.formid });
			in1('pub_form', { _id: uuid(), client_type: v.left.client_type, create_time: v.left.create_time, formid: v.left.formid, formname: v.left.formname, group_number: v.left.group_number, package_no: v.left.package_no, package_name: v.left.package_name, package_path: v.left.package_path, productid: msg.tenantid, group_name: v.left.group_name });
			v.right.forEach((item) => {
				in1('pub_form_code', {
					_id: uuid(), action_name: item.action_name,
					formid: item.formid,
					action_keyword: item.action_keyword,
					code: item.code,
					productid: msg.tenantid
				});
			});
		}
	});
	const res3 = await e1();
	if (res3.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}
